﻿#define _CRT_SECURE_NO_WARNINGS 1
#include "List.h"
int loadStuData(Student data[])
{
	FILE* fp = fopen("student.txt", "r");
	if (fp == NULL)
	{
		return -1;
	}
	int i = 0;
	Student stu;
	while (!feof(fp))
	{
		//fscanf(fp, "%s%s%c%d%lf", data[i].stuId, data[i].name, data[i].gender, data[i].age, data[i].score);
		int ret=fscanf(fp, "%s\t%s\t%c\t%d\t%lf\n", stu.stuId, stu.name, &stu.gender, &stu.age,&stu.score);
		if (ret < 0)
		{
			break;
		}
		data[i] = stu;
		i++;
	}
	return i;
}

void showAStu(Student s)
{
	printf("%s\t%s\t%c\t%d\t%.2lf\n", s.stuId, s.name, s.gender, s.age, s.score);
}
void showAllStu(Student data[], int n)
{
	for (int i = 0; i < n; i++)
		showAStu(data[i]);
	//printf("%s\t%s\t%c\t%d\t%.2lf\n", data[i].stuId, data[i].name, data[i].gender, data[i].age, data[i].score);

}
Student getAStuCyclic(Student data[], int n)
{
	static int cnt = 0;
	return data[(cnt++)% n];
}
Student getAStuRand(Student data[], int n)
{
	srand((unsigned)time(NULL));
	return data[rand()%n];

}
int getLength(ListNode* h)
{
	int length = 0;
	ListNode* cur = h;
	while (cur)
	{
		length++;
		cur = cur->next;
	}
	return length;
}

int insert(ListNode** h, int i, Student x)
{
	ListNode* newnode = (ListNode*)malloc(sizeof(Student));
	newnode->data = x;
	newnode->next = NULL;

	if (i == 1)
	{

		newnode->next=*h;
		*h = newnode;
		return 0;
	}
	if (i > getLength(*h) + 1 || i < 1)
	{
		printf("位置不合法\n");
		return -1;
	}

	ListNode* cur =*h;
	i-=1;
	while (--i)
	{
		cur = cur->next;
	}

	newnode->next = cur->next;
	cur->next = newnode;
	return 0;
}


ListNode* insertFromHeader(ListNode* h, Student x)
{
	ListNode* newnode = (ListNode*)malloc(sizeof(Student));
	newnode->next = NULL;
	newnode->data = x;
	newnode->next = h;
	return newnode;
}

void insertFromFooter(ListNode** ph, Student x)
{
	ListNode* newnode = (ListNode*)malloc(sizeof(Student));
	newnode->data = x;
	newnode->next = NULL;
	if (*ph == NULL)
	{
		*ph=newnode;
	}
	
	ListNode* cur = *ph;
	while (cur->next)
	{
		cur = cur->next;
	}
	cur->next = newnode;
}

void showLinkList(ListNode* h)
{
	if (h == NULL)
	{
		printf("没有数据\n");
	}

	ListNode* cur = h;
	while (cur)
	{
		showAStu(cur->data);
		cur = cur->next;
	}

}

int menu()
{
	
	int item;

	printf("1-显示当前单链表中所有学生的信息\n");
	printf("2-按顺序选择一个学生插入单链表\n");
	printf("3-随机选中一个学生并从头部插入单链表\n");
	printf("4-随机选中一个学生并从尾部插入单链表\n");
	printf("0-退出程序\n");
	printf("请选择一个菜单项：");

	scanf("%d", &item);
	return item;
}


int del(ListNode** h, char stuId[])
{
	ListNode* prev=NULL,*cur=NULL;

	if (*h == NULL)
	{
		return -1;
	}
	
	if (strcmp((*h)->data.stuId, stuId) == 0)
	{
		cur = *h;
		*h = (*h)->next;
		free(cur);
		return 0;
	}

	prev = *h;
	cur = (*h)->next;
	while (cur != NULL)
	{
		if (strcmp(cur->data.stuId, stuId) == 0)
		{
			prev->next = cur->next;
			free(cur);
			return 0;
		}
		prev = cur;
		cur = cur->next;
	}
	return -1;


}
int search(ListNode* h, char stuid[],Student* x)
{
	ListNode* cur = h;
	while (cur != NULL)
	{
		if (strcmp(cur->data.stuId, stuid) == 0)
		{
			*x = cur->data;
			return 0;
		}
		cur = cur->next;
	}

	return -1;
}